<script setup lang="ts">
import {getCurrentInstance, onMounted, reactive} from "vue";
import {useUserStore} from "../../stores/userStore.ts";
import {OAuthControllerService} from "../../../generated";
import User from "../../assets/icons/User.vue";
import Lock from "../../assets/icons/Lock.vue";
import Gitee from "../../assets/icons/Gitee.vue";
import Github from "../../assets/icons/Github.vue";
import Email from "../../assets/icons/Email.vue";

/**
 * 登录表单信息
 */
const loginForm = reactive({
  userAccount: "",
  userPassword: "",
});

/**
 * 注册表单信息
 */
const registerForm = reactive({
  userAccount: "",
  userPassword: "",
  userEmail: ""
});

let userStore = useUserStore();

const instance = getCurrentInstance();
if (!instance) {
  throw new Error('Component instance is null');
}

interface ComponentInstance {
  $Encrypt: (password: string) => string;
}

const proxy = instance.proxy as unknown as ComponentInstance;

const handleLoginSubmit = () => {
  let password = proxy.$Encrypt(loginForm.userPassword);
  userStore.login(loginForm.userAccount, password)
};

const handleRegisterSubmit = () => {
  let password = proxy.$Encrypt(registerForm.userPassword);
  userStore.register(registerForm.userAccount, password, registerForm.userEmail)
}

onMounted(() => {
  const container = document.querySelector(".container");
  const registerBtn = document.querySelector(".register-btn");
  const loginBtn = document.querySelector(".login-btn");

  registerBtn?.addEventListener("click", () => {
    container?.classList.add("active");
  });

  loginBtn?.addEventListener("click", () => {
    container?.classList.remove("active");
  });
});

const handleLoginGitee = async () => {
  let res = await OAuthControllerService.getGiteeAuthUrlUsingGet('login');
  if (res?.code === 0) {
    window.location.href = res.data as string;
  }
}

const handleLoginGithub = async () => {
  let res = await OAuthControllerService.getGithubAuthUrlUsingGet('login');
  if (res?.code === 0) {
    window.location.href = res.data as string;
  }
}
</script>
<template>
  <div class="container">
    <div class="form-box login">
      <div class="form">
        <h1>登录</h1>
        <div class="input-box">
          <input
              v-model="loginForm.userAccount"
              placeholder="用户名"
              required
              type="text"
          />
          <i>
            <User/>
          </i>
        </div>
        <div class="input-box">
          <input
              v-model="loginForm.userPassword"
              placeholder="密码"
              required
              type="password"
          />
          <i>
            <Lock/>
          </i>
        </div>
        <div class="forgot-link">
          <a href="/user/forgot">Forgot Password?</a>
        </div>
        <button class="btn" @click="handleLoginSubmit">提交</button>
        <p>or login with social platforms</p>
        <div class="social-icons">
          <a @click="handleLoginGitee">
            <Gitee/>
          </a>
          <a @click="handleLoginGithub">
            <Github/>
          </a>
        </div>
      </div>
    </div>

    <div class="form-box register">
      <div class="form">
        <h1>注册</h1>
        <div class="input-box">
          <input
              v-model="registerForm.userAccount"
              placeholder="用户名"
              required
              type="text"
          />
          <i>
            <User/>
          </i>
        </div>
        <div class="input-box">
          <input
              v-model="registerForm.userEmail"
              placeholder="邮箱"
              required
              type="text"
          />
          <i>
            <Email/>
          </i>
        </div>
        <div class="input-box">
          <input
              v-model="registerForm.userPassword"
              placeholder="密码"
              required
              type="text"
          />
          <i>
            <Lock/>
          </i>
        </div>
        <div class="forgot-link">
          <a href="/user/forgot">Forgot Password?</a>
        </div>
        <button class="btn" type="submit" @click="handleRegisterSubmit">提交</button>
      </div>
    </div>

    <div class="toggle-box">
      <div class="toggle-panel toggle-left">
        <h1>你好，欢迎来到这里!</h1>
        <p>还没有账号?</p>
        <button class="btn register-btn">去注册</button>
      </div>

      <div class="toggle-panel toggle-right">
        <h1>欢迎回来!</h1>
        <p>已经有帐号了?</p>
        <button class="btn login-btn">去登录</button>
      </div>
    </div>
  </div>
</template>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  position: relative;
  width: 850px;
  height: 550px;
  background: #fff;
  border-radius: 30px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.2);
  margin: 20px;
  overflow: hidden;
}

.form-box {
  position: absolute;
  right: 0;
  width: 50%;
  height: 100%;
  background: #fff;
  display: flex;
  align-items: center;
  color: #333;
  text-align: center;
  padding: 40px;
  z-index: 1;
  transition: 0.6s ease-in-out 1.2s, visibility 0s 1s;
}

.container.active .form-box {
  right: 50%;
}

.form-box.register {
  visibility: hidden;
}

.container.active .form-box.register {
  visibility: visible;
}

.form {
  width: 100%;
}

.container h1 {
  font-size: 36px;
  margin: -10px 0;
}

.input-box {
  position: relative;
  margin: 30px 0;
}

.input-box input {
  width: 100%;
  padding: 13px 50px 13px 20px;
  background: #eee;
  border-radius: 8px;
  border: none;
  outline: none;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.input-box input::placeholder {
  color: #888;
  font-weight: 400;
}

.input-box i {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 20px;
  color: #888;
}

.forgot-link {
  margin: -15px 0 15px;
}

.forgot-link a {
  font-size: 14.5px;
  color: #333;
  text-decoration: none;
}

.btn {
  width: 100%;
  height: 48px;
  background: #3A7BC8;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #fff;
  font-weight: 600;
}

.container p {
  font-size: 14.5px;
  margin: 15px 0;
}

.social-icons {
  display: flex;
  justify-content: center;
}

.social-icons a {
  display: inline-flex;
  padding: 10px;
  border: 2px solid #ccc;
  border-radius: 8px;
  font-size: 24px;
  color: #333;
  text-decoration: none;
  margin: 0 8px;
}

.toggle-box {
  position: absolute;
  width: 100%;
  height: 100%;
}

.toggle-box::before {
  content: "";
  position: absolute;
  left: -250%;
  width: 300%;
  height: 100%;
  background: #3A7BC8;
  border-radius: 150px;
  z-index: 2;
  transition: 1.8s ease-in-out;
}

.container.active .toggle-box::before {
  left: 50%;
}

.toggle-panel {
  position: absolute;
  width: 50%;
  height: 100%;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2;
  transition: 0.6s ease-in-out;
}

.toggle-panel.toggle-left {
  left: 0;
  transition-delay: 1.2s;
}

.container.active .toggle-panel.toggle-left {
  left: -50%;
  transition-delay: 0.6s;
}

.toggle-panel.toggle-right {
  right: -50%;
  transition-delay: 0.6s;
}

.container.active .toggle-panel.toggle-right {
  right: 0;
  transition-delay: 1.2s;
}

.toggle-panel p {
  margin-bottom: 20px;
}

.toggle-panel .btn {
  width: 160px;
  height: 46px;
  background: transparent;
  border: 2px solid #fff;
  box-shadow: none;
}

@media screen and (max-width: 650px) {
  .container {
    height: calc(100vh - 40px);
  }

  .form-box {
    bottom: 0;
    width: 100%;
    height: 70%;
  }

  .container.active .form-box {
    right: 0;
    bottom: 30%;
  }

  .toggle-box::before {
    left: 0;
    top: -270%;
    width: 100%;
    height: 300%;
    border-radius: 20vw;
  }

  .container.active .toggle-box::before {
    left: 0;
    top: 70%;
  }

  .toggle-panel {
    width: 100%;
    height: 30%;
  }

  .toggle-panel.toggle-left {
    top: 0;
  }

  .container.active .toggle-panel.toggle-left {
    left: 0;
    top: -30%;
  }

  .toggle-panel.toggle-right {
    right: 0;
    bottom: -30%;
  }

  .container.active .toggle-panel.toggle-right {
    bottom: 0;
  }
}

@media screen and (max-width: 400px) {
  .form-box {
    padding: 20px;
  }

  .toggle-panel h1 {
    font-size: 30px;
  }
}
</style>
